<!doctype html>
<html>
  <head>
    <title>Code Snippet</title>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="style.css">
	<link type="text/css" rel="stylesheet" href="css/shCore.css"></link>
	<link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"></link>
	<script language="javascript" src="js/shCore.js"></script>
	<script language="javascript" src="js/shBrushJava.js"></script>
	<script type="text/javascript">SyntaxHighlighter.all();</script> 
  </head>
  <body>

<p style="width: 600px; padding: 5px 20px; font-size: 16px;">
The code below is paraphrased version of the real code running on the server. The code below was "inlined" to fit on one page and to be readable. The actual code is represented
by these two files: <a href="http://code.google.com/p/gaetestjig/source/browse/trunk/GaeTestJig/src/gae/testjig/server/ConstraintTestStepper.java">ConstraintTestStepper.java</a>
and <a href="http://code.google.com/p/gaetestjig/source/browse/trunk/GaeTestJig/src/gae/testjig/server/ConstraintTest.java">ConstraintTest.java</a>.
</p>

<script type="syntaxhighlighter" class="brush: java"><![CDATA[
try {
	logger.info(ownerName+": BEGIN");
	
SYNCH(ownerName); // wait for button click in GUI
	logger.info(ownerName+": beginning transaction.");
	datastore = DatastoreServiceFactory.getDatastoreService();
	txn = datastore.beginTransaction();
	
SYNCH(ownerName); // wait for button click in GUI
	logger.info(ownerName+": test for existence of entity with seatId=" + seatId);
	Query testExistsQuery = new Query("Seat");
	testExistsQuery.setAncestor(seatsRootKey);
	testExistsQuery.addFilter("seatId", Query.FilterOperator.EQUAL, seatId);
	Entity exists = datastore.prepare(testExistsQuery).asSingleEntity();
	if ( exists != null ) {
		logger.info(ownerName+": sorry, that seat has already been taken.");
	} else {
		logger.info(ownerName+": that seat has not been taken yet.");

SYNCH(ownerName); // wait for button click in GUI
		logger.info(ownerName+": create the seat entity with seatId=" + seatId);
		Entity seatEntity = new Entity("Seat", seatId, seatsRootKey);
		seatEntity.setProperty("ownerName", ownerName);
		seatEntity.setProperty("seatId", seatId);
		seatEntity.setProperty("timeStamp", System.currentTimeMillis());
		datastore.put(txn, seatEntity);
		seatKey = seatEntity.getKey();
		String key = KeyFactory.keyToString(seatKey);
		logger.info(ownerName+": created seat. entity key = " + key);
	}

SYNCH(ownerName); // wait for button click in GUI
	try {
		txn.commit();
	}
	catch ( Exception e ) {
		throw new CommitException(e);
	}
	finally {
		if (txn.isActive()) {
	        txn.rollback();
	    }
	}
	logger.info(ownerName+": transaction committed.");
	logger.info(ownerName+": END");
}
catch ( AbortException e ) {
	logger.info(ownerName+": ABORT");
}
catch ( CommitException e ) {
	logger.info(ownerName+": commit failed: " + e.getMessage());
	logger.info(ownerName+": ABORT");
}
catch ( Exception e ) {
	logger.info(ownerName+": exception thrown: " + e.getMessage());
	logger.info(ownerName+": ABORT");
}
]]></script>


  </body>
</html>
